package com.runen.wnhz.runen.ui.adapter.puadapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.runen.wnhz.runen.R;
import com.runen.wnhz.runen.bean.LessonRelativeInfoBean;
import com.runen.wnhz.runen.ui.activity.major.LookLessonActivity;

import java.util.List;

public class LessonCatalogPartAdapter extends BaseAdapter {

    private List<LessonRelativeInfoBean.DataBean.TitleListBean.NextListBean> next_list;

    private Context context;

    private String tryLook = "0";

    public LessonCatalogPartAdapter(List<LessonRelativeInfoBean.DataBean.TitleListBean.NextListBean> next_list, Context context, String tryLook) {
        this.next_list = next_list;
        this.context = context;
        this.tryLook = tryLook;
    }

    @Override
    public int getCount() {
        if(next_list != null){
            return next_list.size();
        }else {
            return 0;
        }
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if(convertView == null){
            convertView = View.inflate(context, R.layout.item_catalog_part,null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }
        final LessonRelativeInfoBean.DataBean.TitleListBean.NextListBean nextListBean = next_list.get(position);
        holder.tv_name.setText(nextListBean.getName());
        holder.tv_time.setText(nextListBean.getTime());
        if("1".equals(tryLook)){
            holder.tv_try.setText("试看");
        }else{
            holder.tv_try.setText("观看");
        }
        holder.tv_try.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(context, LookLessonActivity.class);
                intent.putExtra("lid",nextListBean.getLid());
                intent.putExtra("tid",nextListBean.getTid());
                intent.putExtra("nid",nextListBean.getNid());
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    private static class ViewHolder{
        TextView tv_name;
        TextView tv_time;
        TextView tv_try;

        public ViewHolder(View convertView) {
            this.tv_name = convertView.findViewById(R.id.tv_name);
            this.tv_time = convertView.findViewById(R.id.tv_time);
            this.tv_try = convertView.findViewById(R.id.tv_try);
        }
    }
}
